<?php
declare(strict_types=1);

namespace Service\OrderService\Volidate;

use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\Order;
use App\Model\BbcModel\OrderGoods;
use App\Model\BbcModel\OrderExtends;
use Service\PayService\CashPayService;

class CashReturnOrderVolidate extends BaseService
{
    
    /**
     * 收银机退款验证
     * @param int $cid
     * @param int $mn_sid
     * @param int $mid
     * @param array $param
     * @return type
     */
    public function checkReturnOrderData(int $cid ,int $mn_sid,int $mid, array $param = []){
        $order= Order::instance()->info(['id'=>$param['order_id']]);
        if(empty($order)){
            return Normal::returnCode(Normal::FAIL, '原始订单信息未找到');
        }
        if($order['status']==0){
            return Normal::returnCode(Normal::FAIL, '该订单已退货');
        }
        if($order['type']==3&&!isset($param['reason_info'])){
            return Normal::returnCode(Normal::FAIL, '缺少退货原因');
        }
        $param['mid']=$order['mid'];
        $ordergoods= OrderGoods::instance()->lists(['order_id'=>$order['id']]);
        $orderextends= OrderExtends::instance()->info(['order_id'=>$order['id']]);
        $orderData=$order;
        $orderData['goods']=$ordergoods;
        $orderData['orderextends']=$orderextends;
        if($order['type']==3){
            $orderData['reason_info']=$param['reason_info'];
        }
        if(empty($param['pay'])){
            return Normal::returnCode(Normal::FAIL, '支付信息不能为空');
        }
        $pay= @json_decode($param['pay'],TRUE);   
        if(empty($pay)){
            return Normal::returnCode(Normal::FAIL, '支付信息有误');
        }
        $payerCheck = CashPayService::instance()->getReturnPayChannel($pay, $orderData);
        if( $payerCheck['code'] != Normal::SUC ) {
            return $payerCheck;
        }
        
        $param          = $payerCheck['data'];
        
        return Normal::returnCode(Normal::SUC,"获取成功",$param);
        
    }
}
